package com.gh.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gh.enums.OrderStatusEnum;
import com.gh.model.order.OrderInfo;
import com.gh.result.Result;
import com.gh.service.OrderService;
import com.gh.util.AuthContextHolder;
import com.gh.vo.order.OrderCountQueryVo;
import com.gh.vo.order.OrderQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author Eric
 * @Date 2021/6/26 16:03
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderApiController {
    @Autowired
    private OrderService orderService;

    /**
     * 创建订单
     */
    @PostMapping("auth/submitOrder/{scheduleId}/{patientId}")
    public Result submitOrder(@PathVariable String scheduleId,
                              @PathVariable Long patientId){
        Long orderId = orderService.saveOrder(scheduleId,patientId);
        return Result.ok(orderId);
    }
    /**
     * 根据订单id获取订单
     */
    @GetMapping("auth/getOrders/{orderId}")
    public Result getOrders(@PathVariable String orderId){
        OrderInfo orderInfo = orderService.getOrder(orderId);
        return Result.ok(orderInfo);
    }
    /**
     * 订单列表(条件查询带分页)
     */
    @PostMapping("{page}/{limit}")
    public Result orderList(@PathVariable Long page,
                           @PathVariable Long limit,
                           OrderQueryVo orderQueryVo,
                           HttpServletRequest request){
        //设置当前用户id
        orderQueryVo.setUserId(AuthContextHolder.getUserId(request));
        Page<OrderInfo> pageList =new Page<>(page,limit);
        IPage<OrderInfo> pageModel = orderService.selectByPage(pageList,orderQueryVo);
        return Result.ok(pageModel);
    }
    /**
     * 订单状态
     */
    @GetMapping("auth/getStatusList")
    public Result getStatusList(){
        return Result.ok(OrderStatusEnum.getStatusList());
    }
    /**
     * 取消订单
     */
    @GetMapping("auth/cancelOrder/{orderId}")
    public Result cancelOrder(@PathVariable Long orderId){
        Boolean flag = orderService.cancelOrder(orderId);
        return Result.ok(flag);
    }
    /**
     * 获取订单统计数据
     */
    @PostMapping("inner/getCountMap")
    public Map<String ,Object> getCountMap(@RequestBody OrderCountQueryVo orderCountQueryVo){
        return orderService.getCountMap(orderCountQueryVo);
    }
}
